package com.loctek.importexport.util;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.view.Gravity;
import android.view.KeyEvent;
import android.widget.Toast;


import com.loctek.importexport.App;
import com.loctek.importexport.CustomApplication;
import com.loctek.importexport.R;


//import com.gokuai.library.GKApplication;

public class UtilDialog {

    public static void showNormalToast(String message) {
        Toast.makeText(App.getContext(), message, Toast.LENGTH_SHORT).show();
    }

    public static void showNormalToast(int messageRes) {
        Toast.makeText(App.getContext(), messageRes, Toast.LENGTH_SHORT).show();
    }


    private static ProgressDialog loadingDialog;
    private static boolean willShow;

    public static synchronized void dismissLoadingDialog(final Context context) {
        try {
            if (context != null && !((Activity) context).isFinishing() && loadingDialog != null) {
                loadingDialog.dismiss();
                willShow = false;
            }
            loadingDialog = null;
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            loadingDialog = null;
            willShow = false;
        }

    }

    /**
     * 顶部toast
     *
     * @param context
     * @param message
     */
    public static void showTopToast(Context context, String message) {
        Toast toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, -300);
        toast.show();
    }

    /**
     * 顶部toast
     *
     * @param context
     * @param stringRes
     */
    public static void showTopToast(Context context, int stringRes) {
        Toast toat = Toast.makeText(context, stringRes, Toast.LENGTH_SHORT);
        toat.setGravity(Gravity.CENTER_HORIZONTAL, 0, -300);
        toat.show();
    }

    /**
     * 显示正在...对话框
     */
    public static void showDialogLoading(Context context, String message, DialogInterface.OnKeyListener listener, boolean cancelAble) {
        willShow = true;
        dismissLoadingDialog(context);
        loadingDialog = new ProgressDialog(context);
        loadingDialog.setMessage(message);
        loadingDialog.setOnKeyListener(listener);
        loadingDialog.setIndeterminate(true);
        loadingDialog.setCancelable(cancelAble);
        if (!((Activity) context).isFinishing() && willShow) {
            loadingDialog.show();
        }
    }

    public static void showDialogLoading(Context context, String message, boolean cancelAble) {
        showDialogLoading(context, message, null, cancelAble);
    }

    /**
     * 执行loading的任务，需要先执行showDialogLoading，再执行asyncTask
     *
     * @param context
     * @param message
     * @param asyncTask
     */
    public static void showDialogLoading(final Context context, String message, final AsyncTask asyncTask) {

        showDialogLoading(context, message, new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
                    if (asyncTask != null) {
                        asyncTask.cancel(true);
                    }
                    UtilDialog.dismissLoadingDialog(context);
                }
                return false;
            }
        }, false);

    }


    /**
     * 加载Loading
     *
     * @param context
     */
    public static void showDialogLoading(final Context context) {

        showDialogLoading(context, context.getString(R.string.tip_is_loading), new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
                    UtilDialog.dismissLoadingDialog(context);
                }
                return false;
            }
        }, false);

    }


}
